
import java.io.*;
import java.net.*;
import java.util.Scanner;

/**
 * A simple ATMClient for connecting to the ATMServer. Can be used to withdraw money and deposit,
 * there are currently only two languages available.
 * Connects to ATMServer via socket
 * @author Snilledata, Alexander Gomez, Viktor Gavelli
 * @version 1.1
 */
/* public */class ATMClient {
	private static int connectionPort = 8989; 				//the port to connect to 
	private static Languages defaultLang = Languages.ENGLISH;	
	private static ATMLanguage lang = new ATMLanguage(defaultLang);
	private static int WITHCODE = 9;
	
	public static void main(String[] args) throws IOException {

		Socket ATMSocket = null;
		DataOutputStream out = null;
		DataInputStream in = null;
		int loggedIN = 0;
		String adress = "127.0.0.1"; 
		ATMProtocol proto = new ATMProtocol();
		
		
		
		
		try {
			ATMSocket = new Socket(adress, connectionPort);	// Connect to address with specified connection port.
			out = new DataOutputStream(ATMSocket.getOutputStream());	// Prints to server through socket.
			in = new DataInputStream(ATMSocket.getInputStream());	// Read from server through socket.
		} catch (UnknownHostException e) {
			System.err.println("Unknown host: " + adress);
			System.exit(1);
		} catch (IOException e) {
			System.err.println("Couldn't open connection to " + adress);
			System.exit(1);
		}

		System.out.println(lang.getClientMessage(0)); // Connecting...
		System.out.println(in.readUTF()); // read welcome message

		Scanner scanner = new Scanner(System.in); // scanner reads from console
													// input
		int menuOption = 0;
		while (loggedIN < 2) {
			
			System.out.print(lang.getClientMessage(2)); // Username:
			menuOption = scanner.nextInt(); // only accepts int as input,
											// crashes otherwise
			out.writeInt(proto.interpret(menuOption));
			System.out.print(lang.getClientMessage(3)); // Password:
			menuOption = scanner.nextInt();
			out.writeInt(proto.interpret(menuOption));
			loggedIN = proto.interpret(in.readByte());	//receive 0 or 2 
			System.out.println(in.readUTF());		//read welcome message

		}

		System.out.println(lang.getClientMessage(1)); // "(1)Balance, (2)Withdrawal, (3)Deposit, (4)Exit"
		System.out.print("> "); // Generic sign for expecting input. Applies to
								// all languages.

		menuOption = 0;

		/**
		 * Final loop which handles deposits, withdrawals, language changes,
		 * balance and terminates the client.
		 */
		while (menuOption != 5) {
			
			menuOption = scanner.nextInt();
			
			out.writeInt(proto.interpret(menuOption)); // sends userinput to
														// server
			System.out.println(in.readUTF());
			int balance;
			switch (menuOption) {
			case 2:
				System.out.println(lang.getClientMessage(WITHCODE));
            	out.writeInt(scanner.nextInt());
            	if(!in.readBoolean()){
            		System.out.println(in.readUTF());
            		System.out.println(lang.getClientMessage(1));
            		System.out.print("> ");
            		break;
            	}
			case 3:
				System.out.println(lang.getClientMessage(6)); // enter amount
				out.writeInt(proto.interpret(scanner.nextInt()));
			case 1:
				balance = proto.interpret(in.readInt());
				System.out.println(lang.getClientMessage(4) + balance
						+ lang.getClientMessage(5));
				// current balance is X dollar.
				System.out.println(lang.getClientMessage(1)); // "(1)Balance, (2)Withdrawal, (3)Deposit, (4)Exit"
				System.out.print("> ");
				break;
			case 4:
				changeLanguage(scanner);
				System.out.println(lang.getClientMessage(1)); // "(1)Balance, (2)Withdrawal, (3)Deposit, (4)Exit"
				System.out.print("> ");
				break;
			case 5:
				break;
			default:
				break;
			}
		}
		System.out.println(lang.getClientMessage(7)); // Goodbye
		out.close();
		in.close();
		ATMSocket.close();
	}

	/**
	 * Help method for changing language. The actual language change appears in
	 * ATMLanguage.setLanguage(int).
	 * 
	 * @param scanner
	 * @throws IOException
	 */
	private static void changeLanguage(Scanner scanner) throws IOException {
		System.out.println(lang.getAvailableLang());
		lang.setLanguage(scanner.nextInt());
		System.out.println(lang.getClientMessage(8));
	}
}
